<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 放置表单 -->
        <el-form ref="userForm" label-width="120px" :model="userInfo" :rules="rules" style="margin-left: 120px; margin-top:30px">
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfo.username" style="width:300px" />
          </el-form-item>

          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" disabled style="width:300px" />
          </el-form-item>

          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <!-- 使用上传图片组件 -->
            <!-- :url="userInfo.staffPhoto" 将用户信息中的图片路径传递给子组件，避免刷新后子组件中imgUrl中为空 -->
            <!-- <upload-img :url="userInfo.staffPhoto" @img-url="getUrl" /> -->

            <!-- 使用v-modal实现双向绑定，简化父子通信 -->
            <upload-img v-model="userInfo.staffPhoto" />
          </el-form-item>

          <!-- 保存个人信息 -->
          <el-row class="inline-info" type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="confirm">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById, updateUserInfo } from '@/api/user.js'
export default {
  name: 'Detail',
  data() {
    return {
      userInfo: {},
      rules: {
        username: [
          { required: true, message: '名称为必填项', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    // 获取当前查看员工信息
    async getDetail() {
      // 如何获取路由参数？
      const id = this.$route.query.id
      const res = await getUserDetailById(id)
      this.userInfo = res
    },
    // 确认修改
    confirm() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          // 使用最新修改数据覆盖源数据 作为接口提交数据
          await updateUserInfo(this.userInfo)
          this.$message.success('更新成功')
        }
      })
    }
    // 子传父，添加到用户信息中进行上传
    // getUrl(url) {
    //   this.userInfo.staffPhoto = url
    // },

  }
}
</script>
